package com.itheima.ydd.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.ydd.common.R;
import com.itheima.ydd.common.ResultData;
import com.itheima.ydd.dto.YddAdvposDto;
import com.itheima.ydd.entity.YddAdvimg;
import com.itheima.ydd.entity.YddAdvpos;
import com.itheima.ydd.service.IYddAdvimgService;
import com.itheima.ydd.service.IYddAdvposService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author author
 * @since 2022-05-22
 */
@RestController
@RequestMapping("/ydd_advpos")
public class YddAdvposController {
    @Autowired
    private IYddAdvposService iYddAdvposService;
    @Autowired
    private IYddAdvimgService iYddAdvimgService;

    /**
     * 添加广告位
     * @param yddAdvpos
     * @return
     */
    @PostMapping("/add")
    public R save(@RequestBody YddAdvpos yddAdvpos){
        iYddAdvposService.save(yddAdvpos);

        return R.success("添加成功",1);
    }

    /**
     * 分页查询
     * @param advposdesc
     * @param advposname
     * @param page
     * @param limit
     * @return
     */
    @GetMapping("/list")
    public ResultData list(String advposdesc,String advposname,int page, int limit) {
        Page<YddAdvpos> YddAdvposPage = new Page<>(page, limit);
        Page<YddAdvposDto>  yddAdvposDtoPage=new Page<>();
        LambdaQueryWrapper<YddAdvpos> queryWrapper = new LambdaQueryWrapper<>();
        LambdaQueryWrapper<YddAdvimg> yddAdvimgLambdaQueryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.isNotEmpty(advposname), YddAdvpos::getAdvposname, advposname);
        queryWrapper.like(StringUtils.isNotEmpty(advposdesc), YddAdvpos::getAdvposdesc, advposdesc);
        iYddAdvposService.page(YddAdvposPage, queryWrapper);
        //对象拷贝
        BeanUtils.copyProperties(YddAdvposPage,yddAdvposDtoPage,"records");
        List<YddAdvpos> records=YddAdvposPage.getRecords();
        List<YddAdvposDto> list=records.stream().map((item)->{
            YddAdvposDto yddAdvposDto=new YddAdvposDto();
            BeanUtils.copyProperties(item,yddAdvposDto);
//            int advingpos = item.getId();
//            yddAdvimgLambdaQueryWrapper.eq(YddAdvimg::getAdvimgpos,advingpos);
//            int advposnum=yddAdvimgService.count(yddAdvimgLambdaQueryWrapper);
//            yddAdvposDto.setAdvposnum(advposnum);
            return yddAdvposDto;
        }).collect(Collectors.toList());


        for (int i = 0; i < list.size(); i++) {
            YddAdvposDto yddAdvposDto=list.get(i);
            Integer advingpos=yddAdvposDto.getId();
            yddAdvimgLambdaQueryWrapper.eq(YddAdvimg::getAdvimgpos,advingpos);
            int advposnum=iYddAdvimgService.count(yddAdvimgLambdaQueryWrapper);
            yddAdvposDto.setAdvposnum(advposnum);
            yddAdvimgLambdaQueryWrapper.clear();
        }
        yddAdvposDtoPage.setRecords(list);

        HashMap<Object, Object> date = new HashMap<>();
        date.put("count", (int) yddAdvposDtoPage.getTotal());//总记录数
        date.put("totalPages", (int) yddAdvposDtoPage.getPages());//总页数
        date.put("pageSize", (int) yddAdvposDtoPage.getSize());//每页显示记录
        date.put("currentPage", (int) yddAdvposDtoPage.getCurrent());//当前页
        date.put("data", yddAdvposDtoPage.getRecords());//每页数据内容
        return ResultData.ok("", date);
    }

    /**
     * 回显数据
     * @return
     */
    @GetMapping("/rowInfo")
    public ResultData rowInfo(int id){
        YddAdvpos yddAdvpos= iYddAdvposService.getById(id);
        return ResultData.ok("",yddAdvpos);
    }

    /**
     * 广告位修改
     * @param yddAdvpos
     * @return
     */
    @PostMapping("/update")
    public ResultData update(@RequestBody YddAdvpos yddAdvpos){
        iYddAdvposService.updateById(yddAdvpos);

        return ResultData.ok("",null);
    }

    /**
     * 广告位删除
     * @param id
     * @return
     */
    @GetMapping("/del")
    public ResultData delete(int id){
        iYddAdvposService.removeByIdss(id);

        return ResultData.ok("",null);
    }

    /**
     * 查询全部广告位信息
     * @return
     */
    @GetMapping("/all")
    public ResultData all(){
        List<YddAdvpos> list = iYddAdvposService.list();

        return ResultData.ok("",list);
    }
}
